package com.miplus.ccrm.core.controller;

import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JSysUser;
import com.miplus.ccrm.common.PageData;
import com.miplus.ccrm.common.R;
import com.miplus.ccrm.core.api.fastgpt.FastGptService;
import com.miplus.ccrm.core.api.fastgpt.model.ChatRequest;
import com.miplus.ccrm.core.api.filetrans.FileTransApi;
import com.miplus.ccrm.core.api.filetrans.model.FileTransResponseResult;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.aiqm.*;
import com.miplus.ccrm.core.security.JCommonData;
import com.miplus.ccrm.core.service.aiqm.*;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;

/**
 * 智能质检服务接口
 */

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/aiqm")
public class AiQmController {

    @Resource
    private QmRulesService qmRulesService;
    @Resource
    private QmTemplateService templateService;
    @Resource
    private JCommonData jCommonData;
    @Resource
    private QmDataService qmDataService;
    @Resource
    private QmGradeService qmGradeService;
    @Resource
    @Lazy
    private QmModelRulesLinkService qmModelRulesLinkService;
    @Resource
    @Lazy
    private QmQaMainService qmQaMainService;
    @Resource
    private QmQaSubService qmQaSubService;
    @Resource
    private QmFileTaskService qmFileTaskService;

    /**
     * 取质检规则LIST
     */
    @GetMapping("/qmRulesList")
    public R<PageData<QmRulesResVo>> qmRulesList(@Validated QmRulesReqVo reqVo){
        return R.SuccessPage(qmRulesService.qmRulesList(reqVo));
    }

    /**
     * 通过模板取大模型主规则和子规则详细(单个模版的)
     */
    @GetMapping("/qmModelRulesList")
    public R<List<QmModelRulesResVo>> qmModelRulesList(@RequestParam @NotNull Integer tempid) {
        return R.Success(qmModelRulesLinkService.qmModelRulesLinkService(tempid));
    }

    /**
     * 增加质检规则
     */
    @PostMapping("/qmRulesAdd")
    public R<CmdResVo> qmRulesAdd(@RequestBody @Validated QmRulesAddVo addVo){
        //String usrid=jCommonData.getCurrUsrID();
        String usrid = "test";
        return R.Success(qmRulesService.qmRulesAdd(usrid,addVo));
    }

    /**
     * 增加大模型质检主规则
     */
    @PostMapping("/qmModelRulesAdd")
    public R<CmdResVo> qmModelRulesAdd(@RequestBody @Validated QmModelRulesAddVo addVo) {
        //String usrid = jCommonData.getCurrUsrID();
        String usrid = "test";
        return R.Success(qmQaMainService.qmModelRulesAdd(usrid, addVo));
    }

    /**
     * 增加大模型质检子规则
     */
    @PostMapping("/qmModelSubRulesAdd")
    public R<CmdResVo> qmModelSubRulesAdd(@RequestBody @Validated QmModelSubAddVo addVo) {
        //String usrid = jCommonData.getCurrUsrID();
        String usrid = "test";
        return R.Success(qmQaSubService.qmModelSubRulesAdd(usrid, addVo));
    }

    /**
     * 修改大模型主项规则
     */
    @PostMapping("/qmModelRulesEdit")
    public R<CmdResVo> qmModelRulesEdit(@RequestBody @Validated QmModelRulesEditVo editVo) {
        //String usrid = jCommonData.getCurrUsrID();
        String usrid = "test";
        return R.Success(qmQaMainService.qmModelRulesEdit(usrid, editVo));
    }

    /**
     * 修改大模型子项规则
     */
    @PostMapping("/qmModelSubRulesEdit")
    public R<CmdResVo> qmModelSubRulesEdit(@RequestBody @Validated QmModelSubEditVo editVo) {
        //String usrid = jCommonData.getCurrUsrID();
        String usrid = "test";
        return R.Success(qmQaSubService.qmModelSubRulesEdit(usrid, editVo));
    }


    /**
     * 删除大模型质检主规则或者子规则
     */
    @PostMapping("/qmModelRulesDel")
    public R<CmdResVo> qmModelRulesDel(@RequestBody @Validated QmModelRulesDelVo delVo) {
        //String usrid = jCommonData.getCurrUsrID();
        String usrid = "test";
        return R.Success(qmQaMainService.qmModelRulesDel(usrid, delVo));
    }

    /**
     * 修改质检规则
     */
    @PostMapping("/qmRulesEdit")
    public R<CmdResVo> qmRulesEdit(@RequestBody @Validated QmRulesAddVo addVo){
        // String usrid=jCommonData.getCurrUsrID();
        String usrid = "test";
        return R.Success(qmRulesService.qmRulesEdit(usrid,addVo));
    }

    /**
     * 删除质检规则
     */
    @PostMapping("/qmRulesDelete")
    public R<CmdResVo> qmRulesDelete(@RequestBody @Validated QmRulesDelVo delVo){
        return R.Success(qmRulesService.qmRulesDelete(delVo.getRulesid()));
    }

    /**
     * 取质检模板LIST
     */
    @GetMapping("/qmTempList")
    public R<PageData<QmTempResVo>> qmTempList(@Validated QmTempReqVo reqVo){
        return R.SuccessPage(templateService.qmTempList(reqVo));
    }

    /**
     * 增加质检模板
     */
    @PostMapping("/qmTempAdd")
    public R<CmdResVo> qmTempAdd(@RequestBody @Validated QmTempAddVo addVo){
        //String usrid=jCommonData.getCurrUsrID();
        String usrid = "test";
        return R.Success(templateService.qmTempAdd(usrid,addVo));
    }

    /**
     * 修改质检模板
     */
    @PostMapping("/qmTempEdit")
    public R<CmdResVo> qmTempEdit(@RequestBody @Validated QmTempAddVo addVo){
        //String usrid=jCommonData.getCurrUsrID();
        String usrid = "test";
        return R.Success(templateService.qmTempEdit(usrid,addVo));
    }

    /**
     * 删除质检模板
     */
    @PostMapping("/qmTempDelete")
    public R<CmdResVo> qmTempDelete(@RequestBody @Validated QmTempDelVo delVo){
        return R.Success(templateService.qmTempDelete(delVo.getTempid()));
    }

    /**
     * 通过模板取规则
     */
    @GetMapping("/getRulesList")
    public R<List<LabelResVo>> getRulesList(@RequestParam @NotNull Integer tempid){
        return R.Success(qmRulesService.getRulesList(tempid));
    }

    /**
     * 通过模板取规则详细
     */
    @GetMapping("/getRulesDetailList")
    public R<List<QmRulesResVo>> getRulesDetailList(@RequestParam @NotNull Integer tempid){
        return R.Success(qmRulesService.getRulesDetailList(tempid));
    }

    /**
     * 质检任务复核提交
     */
    @PostMapping("/qmCheckResult")
    public R<CmdResVo> qmCheckResult(@RequestBody @Validated QmTaskCheckVo checkVo) {
        // String usrid=jCommonData.getCurrUsrID();
        String usrid = "test";
        return R.Success(qmFileTaskService.checkResult(usrid, checkVo));
    }


    /**
     * 语音文件url链接识别为文字
     */
    @GetMapping("/fileTrans")
    public R<FileTransResponseResult> fileTrans(String fileLink) {
        FileTransResponseResult fileTransResponseResult = FileTransApi.processFileTrans(fileLink);
        return R.Success(fileTransResponseResult);

    }

    /**
     * 创建质检任务
     */
    @PostMapping("/createQmTask")
    public R<CmdResVo> createQmTask(@RequestBody @Validated QmCrtTaskReqVo reqVo) {
        String usrid = "test";
        return R.Success(qmFileTaskService.createQmTask(usrid, reqVo));
    }


    /**
     * 创建质检任务
     */
    @PostMapping("/createQmTask2")
    public R<CmdResVo> createQmTask2(@RequestBody @Validated QmCrtTaskReqVo reqVo) {
        String usrid = "test";
        return R.Success(qmFileTaskService.createQmTask(usrid, reqVo));
    }


    /**
     * 为某个任务启动文本转写
     */
    @PostMapping("/startTranscript")
    public R<CmdResVo> startTranscript(@RequestBody @Validated StartTranscriptReqVo reqVo) {
        return R.Success(qmFileTaskService.startTranscript(reqVo));
    }

    /**
     * 质检任务列表
     */
    @PostMapping("/qmTaskList")
    public R<PageData<QmTaskListResVo>> qmTaskList(@RequestBody @Validated QmTaskReqVo reqVo) {
        return R.SuccessPage(qmFileTaskService.qmTaskList(reqVo));
    }

    /**
     * 取某个质检任务下的详情
     */
    @GetMapping("/qmTaskDetail")
    public R<QmTaskResVo> qmTaskDetail(@RequestParam @NotNull String qmtaskid) {
        return R.Success(qmFileTaskService.qmTaskDetail(qmtaskid));
    }


    /**
     * 提交大模型质检任务
     */
    @PostMapping("/submitQmTask")
    public R<CmdResVo> submitQmTask(@RequestBody @Validated QmSubmitTaskReqVo reqVo) throws NoApiKeyException, InputRequiredException {
        return R.Success(qmFileTaskService.submitQmTask(reqVo));
    }

    /**
     * 查询测试录音
     */
    @GetMapping("/callHistList")
    public R<PageData<CallResVo>> callHistList(@Validated CallReqVo callReqVo){
        return R.SuccessPage(qmFileTaskService.recordList(callReqVo));
    }

    /**
     * 测试判断坐席回答是否为符合业务知识
     */
    @PostMapping("/testAnswer")
    public R<TestAnswerResVo> testAnswer(@RequestBody @Validated TestAnswerReqVo reqVo) {
        ChatRequest chatRequest = new ChatRequest();
        chatRequest.setStream(false);
        ChatRequest.Message message = new ChatRequest.Message();
        message.setRole("user");
        message.setContent(reqVo.getDialogue());
        chatRequest.setMessages(Collections.singletonList(message));
        String content = FastGptService.sendChatRequest(chatRequest, "fastgpt-iQHo1tJTU7ikxnEK7RB6wJPtdcSAVX5t005ozSWI3JQQVth7PG3biuvmOf81df9")
                .getChoices().get(0).getMessage().getContent();
        TestAnswerResVo testAnswerResVo = new TestAnswerResVo();
        testAnswerResVo.setContent(content);
        return R.Success(testAnswerResVo);
    }
}
